<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\shop\model;

use think\Db;
use think\Model;

class CarModel extends Model
{
    /**
     * @param $good_id 商品ID
     * @param $user_id 用户ID
     * @param $type 类型 type 1 增加 0减少 2删除
     */

    public function edit_car($good_id,$user_id,$type){
        $where=[
          'good_id'=>$good_id,
          'user_id'=>$user_id,
        ];
        $car_info=Db::name('car')->where($where)->find();
        if(isset($car_info)&&$car_info){
            if($type==1){
                Db::name('car')->where('id', $car_info['id'])->setInc('num');
            }
            if($type==0){
                if($car_info['num']>1){
                    Db::name('car')->where('id', $car_info['id'])->setDec('num');
                }

            }
            if($type==2){
                Db::name('car')->where('id', $car_info['id'])->delete();
            }
        }else{
            $where['num']=1;
            $where['is_checked']=1;
            Db::name('car')->insert($where);
        }
    }

    /**
     * 统计购物车数量
     * @param $user_id
     * @return float|int
     */
    function count_mum($user_id,$good_id=null){
        if($good_id){
            $where['user_id']=$user_id;
            $where['good_id']=$good_id;
            $rs=Db::name('car')->where($where)->find();
            $num=$rs['num'];
        }else{
            $num=Db::name('car')->where('user_id', $user_id)->sum('num');
        }

        return $num;
    }
     function goods(){
         return $this->belongsTo('GoodsModel', 'good_id','id')->setEagerlyType(1);

    }
    function get_car_list($user_id){
        $car_list =$this->where('user_id',$user_id)->with('goods')->select()->toArray();
        return $car_list;
    }

    function edit_select($good_id,$type,$user_id){
        if($good_id!=0&&$type==2){
            $where=[
                'user_id'=>$user_id,
                'good_id'=>$good_id,
            ];
            $rs=Db::name('car')->where($where)->find();
            $is_select=$rs['is_checked']==1?0:1;
            Db::name('car')->where($where)->update(['is_checked'=>$is_select]);
        }
        //全选中
        if($type==1) {
            $where=[
                'user_id'=>$user_id
            ];
            Db::name('car')->where($where)->update(['is_checked'=>1]);
        }
        //全不选中
        if($type==0) {
            $where=[
                'user_id'=>$user_id
            ];
            Db::name('car')->where($where)->update(['is_checked'=>0]);
        }
    }

    function del_car($good_id,$user_id){
        $where=[
            'user_id'=>$user_id,
            'good_id'=>$good_id,
        ];
        Db::name('car')->where($where)->delete();
    }

}
